package com.hsy.linemoney.apiservice;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;

import com.hsy.linemoney.R;
import com.hsy.linemoney.beans.CountryPair;
import com.hsy.linemoney.beans.LangPair;
import com.hsy.linemoney.beans.User;
import com.hsy.linemoney.common.Consts;
import com.liferay.mobile.android.auth.basic.BasicAuthentication;
import com.liferay.mobile.android.service.SessionImpl;
import com.liferay.mobile.android.service.Session;

import com.hsy.linemoney.application.Application;

/**
 * LineMoney上下文，保存应用中的一些临时变量
 * Created by MartinKent on 2016/9/9.
 */
public class LineMoneyContext {

    /**
     * Application
     */
    private static final String lineMoneyContextSharedPreferenceName = "lineMoneyContextSharedPreferenceName@!#&*%2353!$&@*(#896";
    private static Application application;
    private static String password;

    public static Context getApplication() {
        return application;
    }

    /**
     * server,liferay_company_id, liferay_group_id
     */
    public static String server;
    public static String liferay_company_id;
    public static String liferay_group_id;

    public static String getServer() {
        if (null == server) {
            server = application.getString(R.string.liferay_server);
        }
        return server;
    }

    public static void setServer(String server) {
        LineMoneyContext.server = server;
    }

    public static String getLiferay_company_id() {
        if (null == liferay_company_id) {
            liferay_company_id = String.valueOf(application.getResources().getInteger(R.integer.liferay_company_id));
        }
        return liferay_company_id;
    }

    public static void setLiferay_company_id(String liferay_company_id) {
        LineMoneyContext.liferay_company_id = liferay_company_id;
    }

    public static String getLiferay_group_id() {
        if (null == liferay_group_id) {
            liferay_group_id = String.valueOf(application.getResources().getInteger(R.integer.liferay_group_id));
        }
        return liferay_group_id;
    }

    public static void setLiferay_group_id(String liferay_group_id) {
        LineMoneyContext.liferay_group_id = liferay_group_id;
    }

    /**
     * localCountryPair, localLanguagePair
     */
    public static CountryPair localCountryPair;
    public static LangPair localLanguagePair;

    public static void setLocalLanguagePair(LangPair pair) {
        checkInit();
        localLanguagePair = pair;
        application.getSharedPreferences(lineMoneyContextSharedPreferenceName, Context.MODE_APPEND).edit().putString(Consts.Keys.LOCAL_LANG, pair.toString()).commit();
    }

    public static LangPair getLocalLanguagePair() {
        checkInit();
        if (null == localLanguagePair) {
            String src = application.getSharedPreferences(lineMoneyContextSharedPreferenceName, Context.MODE_APPEND).getString(Consts.Keys.LOCAL_LANG, null);
            if (null != src) {
                localLanguagePair = LangPair.fromString(src, LangPair.class);
            }
        }
        return localLanguagePair;
    }

    public static void removeLocalLanguagePair() {
        checkInit();
        application.getSharedPreferences(lineMoneyContextSharedPreferenceName, Context.MODE_APPEND).edit().remove(Consts.Keys.LOCAL_LANG).commit();
        localLanguagePair = null;
    }

    public static void changeLocalLanguage(LangPair pair) {
        setLocalLanguagePair(pair);

        Resources resources = application.getResources();
        Configuration config = resources.getConfiguration();
        config.locale = pair.locale;
        DisplayMetrics dm = resources.getDisplayMetrics();
        resources.updateConfiguration(config, dm);
    }

    public static void setLocalCountryPair(CountryPair pair) {
        checkInit();
        localCountryPair = pair;
        application.getSharedPreferences(lineMoneyContextSharedPreferenceName, Context.MODE_APPEND).edit().putString(Consts.Keys.LOCAL_COUNTRY, pair.toString()).commit();
    }

    public static CountryPair getLocalCountryPair() {
        checkInit();
        if (null == localCountryPair) {
            String src = application.getSharedPreferences(lineMoneyContextSharedPreferenceName, Context.MODE_APPEND).getString(Consts.Keys.LOCAL_COUNTRY, null);
            if (null != src) {
                localCountryPair = CountryPair.fromString(src, CountryPair.class);
            }
        }
        return localCountryPair;
    }

    public static void removeLocalCountryPair() {
        checkInit();
        application.getSharedPreferences(lineMoneyContextSharedPreferenceName, Context.MODE_APPEND).edit().remove(Consts.Keys.LOCAL_COUNTRY).commit();
        localCountryPair = null;
    }

    /**
     * session, loggedUser
     */
    private static Session session;
    private static User loggedUser;

    public static Session getSession() {
        checkInit();
        return session;
    }

    public static void setSession(Session session) {
        LineMoneyContext.session = session;
    }

    public static User getLoggedUser() {
        checkInit();
        return loggedUser;
    }

    public static void setLoggedUser(User loggedUser) {
        checkInit();
        LineMoneyContext.loggedUser = loggedUser;
    }

    public static void init(@NonNull Application app) {
        application = app;

        getServer();
        getLiferay_company_id();
        getLiferay_group_id();
        getLocalCountryPair();
        getLocalLanguagePair();

        session = new SessionImpl(server, new BasicAuthentication(Consts.SUPER_USER, Consts.SUPER_USER_PASS));
    }

    public static void checkInit() {
        if (null == application) {
            throw new RuntimeException("LineMoneyContext not inited");
        }
    }

    public static boolean isLocalSetted() {
        checkInit();
        return null != localCountryPair && null != localLanguagePair;
    }

    public static boolean isLoggedIn() {
        checkInit();
        return null != session && null != loggedUser;
    }

    public static void clearData() {
        DataHolder.clearData();
    }

    public static void logout() {
        checkInit();
        session = new SessionImpl(server, new BasicAuthentication(Consts.SUPER_USER, Consts.SUPER_USER_PASS));
        loggedUser = null;
        DataHolder.clearData();
    }

    public static void setPassword(String password) {
        LineMoneyContext.password = password;
    }

    public static String getPassword() {
        return password;
    }
}